﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GovernmentOfficeProject.LevelDataAccess;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.SysTools;

namespace GovernmentOfficeProject.LevelBusinessLogic
{
    public class BLL_USER
    {

        //增加用户
        public bool addUserByEntity(Entity_USER_User userEntity)
        {
            try
            {
                return new DAL_USER_User().addUser(userEntity);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        //增加用户对应部门LIST
        public bool addUserAndDeptlist(Entity_USER_User userEntity,List<Entity_USER_Department> deptEntityList)
        {
            try
            {
                bool flag = false;
                DAL_USER_UserDept userDeptADL = new DAL_USER_UserDept();
                AppLog.Write(deptEntityList.Count.ToString(), AppLog.LogMessageType.Debug);
                AppLog.Write(userEntity.UserID.ToString(), AppLog.LogMessageType.Debug);
                foreach (Entity_USER_Department dept in deptEntityList)
                {
                    //AppLog.Write("addUserAndDeptlist-foreach", AppLog.LogMessageType.Debug);
                    Entity_USER_UserDept userDept = new Entity_USER_UserDept();
                    //AppLog.Write("addUserAndDeptlist-userDept-new", AppLog.LogMessageType.Debug);
                    userDept.UserID = userEntity.UserID;
                    //AppLog.Write("addUserAndDeptlist-userDept.UserID", AppLog.LogMessageType.Debug);
                    userDept.DeptID = dept.DeptID;
                    AppLog.Write("addUserAndDeptlist" + "uid=" + userDept.UserID + ",did=" + userDept.DeptID, AppLog.LogMessageType.Debug);
                    if (!userDeptADL.addUserDept(userDept))
                    {
                        throw new Exception("添加用户对应部门出现异常.");
                    }
                    else
                    {
                        flag = true;
                    }
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }

        //增加用户对应用户组(角色)LIST
        public bool addUserAndRolelist(Entity_USER_User userEntity, List<Entity_USER_Role> roleEntityList)
        {
            try
            {
                bool flag = false;
                //DAL_USER_UserDept userDeptADL = new DAL_USER_UserDept();
                DAL_USER_UserRole userRoleDLL = new DAL_USER_UserRole();
                AppLog.Write(roleEntityList.Count.ToString(), AppLog.LogMessageType.Debug);
                AppLog.Write(userEntity.UserID.ToString(), AppLog.LogMessageType.Debug);
                foreach (Entity_USER_Role role in roleEntityList)
                {
                    Entity_USER_UserRole userRole = new Entity_USER_UserRole();
                    userRole.UserID = userEntity.UserID;
                    userRole.RoleID = role.RoleID;
                    if (userRoleDLL.addUserRole(userRole))
                    {
                        flag = true;                       
                    }
                    else
                    {
                        throw new Exception("设置用户关联用户组错误.");
                    }
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }

        //根据用户id,删除部门关系.
        public bool deleteUserDeptByUserID(decimal userID)
        {
            try
            {
                return new DAL_USER_UserDept().deleteUserDeptByUserID(userID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        //根据用户id和部门id,删除部门关系.
        public bool deleteUserDeptByUserIDAndDeptID(decimal userID,decimal deptID)
        {
            try
            {
                return new DAL_USER_UserDept().deleteUserDeptByUserIDAndDeptID(userID, deptID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        //根据用户id,删除用户组(角色)关系.
        public bool deleteUserRoleByUserID(decimal userID)
        {
            try
            {
                return new DAL_USER_UserRole().deleteUserRoleByUserID(userID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        //根据用户id和角色id,删除角色关系.
        public bool deleteUserRoleByUserIDAndRoleID(decimal userID, decimal roleID)
        {
            try
            {
                return new DAL_USER_UserRole().deleteUserRoleByUserIDAndRoleID(userID, roleID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        //删除指定用户-------------------------------------------------------------需要同时删除相关联表的数据.---未处理.
        public bool deleleUserByUserID(decimal userID)
        {
            try
            {
 //               AppLog.Write("deleteBLL", AppLog.LogMessageType.Debug);
                return new DAL_USER_User().deleteUserByUserID(userID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        //删除多个选定用户
        public bool deleteUserListByUserIdList(List<decimal> userIdList)
        {
            try
            {
                bool flag = false;
                DAL_USER_User dalUsesr = new DAL_USER_User();
                foreach (decimal userID in userIdList)
                {
                    flag = dalUsesr.deleteUserByUserID(userID);
                    //如果有删除失败但最后一个是成功怎么处理,是否用事务控制所有都成功或都失败.
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }


        //修改一个用户,根据传递的实体字段参数修改.
        public bool updateUserByUserID(decimal userID,Entity_USER_User userEntity)
        {
            try
            {
                return new DAL_USER_User().updateUserByUserID(userID, userEntity);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }


        //获取一个用户,根据用户参数实体.
        public Entity_USER_User getUserByUser(Entity_USER_User userEntity)
        {
            try
            {
                //AppLog.Write("BLL"+userEntity.UserID.ToString(), AppLog.LogMessageType.Debug);
                return new DAL_USER_User().getUserByUserID(userEntity.UserID);
            }
            catch (System.Exception ex)
            {                
                throw ex;
            }
        }

        //获取一个用户,根据用户ID.
        public Entity_USER_User getUserByUserID(decimal userID)
        {
            try
            {
                return new DAL_USER_User().getUserByUserID(userID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        //获取指定条件用户列表,多选查询.
        public List<Entity_USER_User> getUserListByCondition(Entity_USER_User userEntity)
        {
            try
            {
                return new DAL_USER_User().getUserListByUserEntity(userEntity);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        
        //获取所有用户
        public List<Entity_USER_User> getAllUser()
        {
            try
            {
                return new DAL_USER_User().getUserList();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        //获得系统联系人分页列表数据
        public Grid<Entity_USER_User> getUserGridData(Entity_USER_User user, int currentPage, int pageSize)
        {
            Grid<Entity_USER_User> grid = new Grid<Entity_USER_User>();
            try
            {
                DAL_USER_User dal = new DAL_USER_User();
                List<Entity_USER_User> userList = dal.getUserPaginationList(user, currentPage, pageSize);
                foreach (Entity_USER_User u in userList)
                {
                    string deptNames = "";
                    List<Entity_USER_Department> deptList = new BLL_Department().getDeptListByUser(u);
                    foreach (Entity_USER_Department dept in deptList)
                    {
                        deptNames += dept.DeptName + ",";
                    }
                    if (deptNames.Length > 0)
                    {
                        deptNames = deptNames.Substring(0, deptNames.Length - 1);
                    }
                    else
                    {
                        deptNames = "未设置";
                    }
                    u.DeptNames = deptNames;
                }
                grid.rows = userList;
                grid.total = dal.getAllCount(user);
                return grid;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        //根据部门ID获取部门下所有用户.
        public List<Entity_USER_User> getUserListByDeptID(decimal deptID)
        {
            try
            {
                return new DAL_USER_User().getUserListByDeptID(deptID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        //获取用户对应部门list
        public List<Entity_USER_Department> getDeptByUser(Entity_USER_User userEntity)
        {
            try
            {
                return new DAL_USER_Department().getDeptListByUserID(userEntity.UserID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        //获取用户对应的用户组list
        public List<Entity_USER_Role> getRoleByUser(Entity_USER_User userEntity)
        {
            try
            {
                return new DAL_USER_Role().getRoleListByUserID(userEntity.UserID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

   #region 用于datasource控件的方法.好像没有被使用
        //增加用户
        public bool addUserByEntity(decimal userID, string loginName, string loginPWD, string realName, string sex, string mobilePhone, string telephony, string email, string qq, string officeAddress, string homeAddress, string lastLoginTime, decimal createUserID, string CreateDate, decimal modifyUserID, string modifyDate, decimal recordStatus, string backup1, string backup2, string backup3, string backup4, string backup5)
        {
            try
            {   
                Entity_USER_User userEntity = new Entity_USER_User();
                userEntity.UserID = userID;
                userEntity.LoginName = loginName;
                userEntity.LoginPassword = loginPWD;
                userEntity.RealName = realName;
                userEntity.Sex = sex;
                userEntity.MobilePhone = mobilePhone;
                userEntity.Telephony = telephony;
                userEntity.Email = email;
                userEntity.QQ = qq;
                userEntity.OfficeAddress = officeAddress;
                userEntity.HomeAddress = homeAddress;
                userEntity.LastLoginTime = lastLoginTime;
                userEntity.CreateUserID = createUserID;
                userEntity.CreateDateTime = CreateDate;
                userEntity.ModifyUserID = modifyUserID;
                userEntity.ModifyDateTime = modifyDate;
                userEntity.RecordStatus = recordStatus;
                userEntity.Backup1 = backup1;
                userEntity.Backup2 = backup2;
                userEntity.Backup3 = backup3;
                userEntity.Backup4 = backup4;
                userEntity.Backup5 = backup5;
                return new DAL_USER_User().addUser(userEntity);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
		 
	#endregion

    }
}
